<template>
  <BasicForm @register="registerForm"/>
</template>
<script lang="ts" name="UserForm" setup>
import {BasicForm, useForm} from '@/components/form/index';
import {formSchema} from '../data/data';
import {Api, getUserInfo} from '../data/api';

//表单配置
const [registerForm, {setProps, resetFields, setFieldsValue, formSubmit, updateSchema}] = useForm({
  rowKey: 'userId',
  saveApi: Api.save,
  updateApi: Api.update,
  params: {
    userType: 'USER'
  },
  labelWidth: 120,
  schemas: formSchema,
  baseColProps: {
    span: 22,
  },
  showActionButtonGroup: false,
});

/**
 * 表单赋值
 * @param data
 */
async function setData(data) {
  await resetFields();
  if (typeof data.record === 'object') {
    let record = data.record;
    let userId = record.userId;
    if (userId) {
      let userInfo = await getUserInfo({userId: userId});
      if (userInfo) {
        await setFieldsValue({
          ...userInfo,
        });
        return;
      }
    }
  }
  // 隐藏底部时禁用整个表单
  //await setProps({ disabled: !showFooter })
}

/**
 * 提交事件
 */
async function handleSubmit(options) {
  await formSubmit(options);
}

/**
 * 暴露给父组件调用的方法
 */
defineExpose({
  setData,
  handleSubmit,
});
</script>
